/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.ts.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

import com.xy.biz.ts.domain.bo.ExaminationQuestionBO;
import com.xy.biz.ts.domain.converter.ExaminationPaperRuleConverter;
import com.xy.biz.ts.domain.query.ExaminationQuestionQuery;
import com.xy.biz.ts.manager.ExaminationQuestionManager;
import com.xy.tool.context.UserContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xy.lang.domain.Result;
import com.xy.biz.ts.domain.bo.ExaminationPaperRuleBO;
import com.xy.biz.ts.domain.query.ExaminationPaperRuleQuery;
import com.xy.biz.ts.service.ExaminationPaperRuleService;
import com.xy.biz.ts.manager.ExaminationPaperRuleManager;
import org.springframework.util.CollectionUtils;

/**
 *
 *
 * @author wwj
 * @date 2021-3-25
 */
@Service
public class ExaminationPaperRuleServiceImpl implements ExaminationPaperRuleService {

    @Autowired
    private ExaminationPaperRuleManager examinationPaperRuleManager;

    @Autowired
    private ExaminationQuestionManager examinationQuestionManager;

    @Override
    public Result<?> saveExaminationPaperRule(ExaminationPaperRuleBO examinationPaperRuleBO) {
        examinationPaperRuleBO.setTenantId(UserContextHolder.getTenantId());
        ExaminationQuestionQuery query = ExaminationQuestionQuery.max()
                .category(examinationPaperRuleBO.getQuestionCategory())
                .type(examinationPaperRuleBO.getQuestionType())
                .difficulty(examinationPaperRuleBO.getDifficulty())
                .score(examinationPaperRuleBO.getScore()).build();
        ExaminationPaperRuleQuery examinationPaperRuleQuery = ExaminationPaperRuleConverter.toQuery(examinationPaperRuleBO);
        examinationPaperRuleQuery.setCount(null);
        ExaminationPaperRuleBO existedExaminationPaperRuleBO = examinationPaperRuleManager.getExaminationPaperRuleByQuery(examinationPaperRuleQuery);
        if (Objects.nonNull(existedExaminationPaperRuleBO)) {
            examinationPaperRuleBO.setCount(examinationPaperRuleBO.getCount() + existedExaminationPaperRuleBO.getCount());
        }
        List<ExaminationQuestionBO> examinationQuestionBOS = examinationQuestionManager.listExaminationQuestion(query);
        if (CollectionUtils.isEmpty(examinationQuestionBOS)) {
            return Result.fail(null, "题库中没有满足条件的试题！");
        }
        if (examinationQuestionBOS.size() < examinationPaperRuleBO.getCount()) {
            return Result.fail(null, "题库中满足条件的试题数量不足！");
        }
        int affectedRows;
        if (Objects.nonNull(existedExaminationPaperRuleBO)) {
            ExaminationPaperRuleBO examinationPaperRuleBO1 = ExaminationPaperRuleBO.builder()
                    .examinationPaperRuleId(existedExaminationPaperRuleBO.getExaminationPaperRuleId())
                    .examinationPaperId(existedExaminationPaperRuleBO.getExaminationPaperId())
                    .count(examinationPaperRuleBO.getCount())
                    .tenantId(UserContextHolder.getTenantId())
                    .build();
            affectedRows = examinationPaperRuleManager.updateExaminationPaperRule(examinationPaperRuleBO1);
        } else {
            affectedRows = examinationPaperRuleManager.saveExaminationPaperRule(examinationPaperRuleBO);
        }
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public Result<?> saveAllExaminationPaperRule(Collection<ExaminationPaperRuleBO> collection) {
        int affectedRows = examinationPaperRuleManager.saveAllExaminationPaperRule(collection);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public Result<?> updateExaminationPaperRule(ExaminationPaperRuleBO examinationPaperRuleBO) {
        int affectedRows = examinationPaperRuleManager.updateExaminationPaperRule(examinationPaperRuleBO);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public Result<?> deleteExaminationPaperRule(Long examinationPaperRuleId) {
        int affectedRows = examinationPaperRuleManager.deleteExaminationPaperRule(examinationPaperRuleId);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public ExaminationPaperRuleBO getExaminationPaperRule(Long examinationPaperRuleId) {
        return examinationPaperRuleManager.getExaminationPaperRule(examinationPaperRuleId);
    }

    @Override
    public List<ExaminationPaperRuleBO> listExaminationPaperRuleById(Collection<Long> collection) {
        return examinationPaperRuleManager.listExaminationPaperRuleById(collection);
    }

    @Override
    public List<ExaminationPaperRuleBO> listExaminationPaperRule(ExaminationPaperRuleQuery query) {
        List<ExaminationPaperRuleBO> examinationPaperRuleBOS = examinationPaperRuleManager.listExaminationPaperRule(query);
        examinationPaperRuleBOS.forEach(
                r -> r.setTotalScore(r.getScore() * r.getCount())
        );
        return examinationPaperRuleBOS;
    }

    @Override
    public int countExaminationPaperRule(ExaminationPaperRuleQuery query) {
        return examinationPaperRuleManager.countExaminationPaperRule(query);
    }

}
